import {
  coupon
} from '../../../../js/modules/market';
import BasePage from '../../../../js/common/base-page'
import { util } from '../../../../js/util';
import { confirm } from '../../../../js/newUtil';
BasePage({
  pageKey: "couponDetail",
  /**
   * 页面的初始数据
   */
  data: {
    couponsId: 0,
    discountValue: 0,
    isApply:0,
    issueName:"",
    minimumValue:0,
    titleName:"",
    pullEndTime:"",
    pullStarTime:"",
    termDay:0,
    discountMode:0,
    terminal:0,
    termType:0,
    drawCount:0,
    pullCount:0,
    quantity:0,
    pullMaxCount:0,
    isLogin:0,
    FKId:0,
    startTime:"",
    endTime:"",
    couponId:0,
    RestrictNum:0,
    isOut:false,  //是否已领完
    buttonControl: {},
    integration: 0, // 兑换优惠券所需积分
    grantType: 0, // 为5时为积分兑换
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.couponsid = options.couponsId || options.couponsid;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadCouponsDetail();
 
  },
  loadCouponsDetail(){
    var that=this;
    var couponsId = that.data.couponsid;
    coupon.getCouponsDetail(couponsId,function (res) {
      if (res.count!=0){
        var data=res.data[0];
        let receiveStatus = that.calculateState(data)
        let buttonControl = that.parseReceiveStatus(receiveStatus, data.GrantType)
        console.log(buttonControl)
        that.setData({        
          discountValue: data.DiscountValue,
          isApply: data.IsApply,
          issueName: data.IssueName,
          minimumValue: data.MinimumValue,
          titleName: data.Name,
          pullEndTime: data.PullEndTime,
          pullStarTime: data.PullStarTime,
          termDay: data.TermDay,
          discountMode: data.DiscountMode,
          terminal: coupon.praseTerminalName(data.Terminal),
          termType: data.TermType,
          drawCount: parseFloat(data.DrawCount),
          pullCount: parseFloat(data.PullCount),
          quantity: parseFloat(data.Quantity),
          pullMaxCount: parseFloat(data.PullMaxCount),
          RestrictNum: parseFloat(data.RestrictNum),
          isLogin: data.IsLogin,
          FKId: data.FKId,
          startTime: data.StartTime||'',
          endTime: data.EndTime||'',
          couponId: data.CouponId,
          buttonControl: buttonControl,
          integration: data.Integration || 0,
          grantType: data.GrantType || 0
        });
      }else{
        that.setData({
          isOut:true
        })
      }
    });
  },

  calculateState (model) {
    let receiveStatus = 0
    if (parseFloat(model.DrawCount) >= parseFloat(model.Quantity)) {
      receiveStatus = 4 // 已领完
    } else if (model.PullCount == 0 || model.IsLogin == '0') {
      receiveStatus = 1 // 立即领取
    } else {
      if (model.EveryDayNum == 0) {
        if ((parseFloat(model.PullCount) < parseFloat(model.RestrictNum)) || (parseFloat(model.RestrictNum) == 0)) {
          receiveStatus = 2 // 继续领取
        } else {
          receiveStatus = 3 // 立即领取
        }
      } else {
        if (parseFloat(model.IsAgainPull) == 1) {
          receiveStatus = 1 // 立即领取
        } else {
          receiveStatus = 3 // 立即领取
        }
      }
    }
    return receiveStatus
  },

  parseReceiveStatus(receiveStatus, grantType) {
    let result = {
      status: receiveStatus || 0,
      text: '',
      btnShow: false,
      btnText: '',
      enable: true
    }
    switch (result.status) {
      case 0:
        break; // 不显示
      case 1:
        result.btnShow = true
        result.btnText = '立即领取'
        break; // 立即领取
      case 2:
        result.btnShow = true
        result.btnText = '继续领取'
        break; // 继续领取
      case 3:
        result.btnShow = true
        result.btnText = '立即领取'
        break; // 立即领取
      case 4:
        result.text = '已领完'
        break; // 已领完
      case 5:
        result.text = '未使用'
        break; // 未使用
      case 6:
        result.text = '已使用'
        result.enable = false // 不可用，为灰色
        break; // 已使用
      case 7:
        result.text = '已过期'
        result.enable = false // 不可用，为灰色
        break; // 已过期
      case 8:
        result.text = '已选中'
        break; // 选中
    }
    if (grantType == 5) {
      // 积分兑换优惠券，替换显示文字
      result.btnText = result.btnText.replace(/领取/img, '兑换')
      result.btnText = result.btnText.replace(/领/img, '兑')
    }
    return result
  },

  /**领取优惠劵 */
  receivedCoupon: async function(evt) {
    var that = this;
    var couponId = that.data.couponId;
    var fkid = that.data.FKId;
    let {
      integration,
      grantType
    } = this.data
    if (grantType == 5) {
      // 积分兑换时，需要用户二次确认是否消耗积分兑换
      let confirmRes = await confirm({
        content: `请确认是否需要使用${integration}积分兑换该优惠券，兑换后积分不会退还，是否兑换？`
      })
      if (!confirmRes.confirm) {
        return
      }
    }
    coupon.onlineReceivedCoupon(couponId, fkid, function (res) {
      if (grantType == 5) {
        util.success("兑换成功！");
      } else {
        util.success("领取成功！");
      }
      that.loadCouponsDetail();
    }, grantType);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})